# uncompyle6 version 3.2.3
# Python bytecode 3.6 (3379)
# Decompiled from: Python 3.6.8 |Anaconda custom (64-bit)| (default, Feb 21 2019, 18:30:04) [MSC v.1916 64 bit (AMD64)]
# Embedded file name: site-packages\PyInstaller\utils\hooks\django.py
import os
from ..hooks import eval_script
from ...utils import misc


def django_dottedstring_imports(django_root_dir):
    """
    Get all the necessary Django modules specified in settings.py.
    
    In the settings.py the modules are specified in several variables
    as strings.
    """
    pths = []
    pths.append(misc.get_path_to_toplevel_modules(django_root_dir))
    pths.append(django_root_dir)
    package_name = os.path.basename(django_root_dir) + ".settings"
    env = {"DJANGO_SETTINGS_MODULE": package_name, "PYTHONPATH": os.pathsep.join(pths)}
    ret = eval_script("django_import_finder.py", env=env)
    return ret


def django_find_root_dir():
    """
    Return path to directory (top-level Python package) that contains main django
    files. Return None if no directory was detected.
    
    Main Django project directory contain files like '__init__.py', 'settings.py'
    and 'url.py'.
    
    In Django 1.4+ the script 'manage.py' is not in the directory with 'settings.py'
    but usually one level up. We need to detect this special case too.
    """
    from ...config import CONF

    manage_py = CONF["main_script"]
    manage_dir = os.path.dirname(os.path.abspath(manage_py))
    settings_dir = None
    files = set(os.listdir(manage_dir))
    if "settings.py" in files or "settings" in files:
        if "urls.py" in files:
            settings_dir = manage_dir
        for f in files:
            if os.path.isdir(os.path.join(manage_dir, f)):
                subfiles = os.listdir(os.path.join(manage_dir, f))
                if "settings.py" in subfiles or "settings" in subfiles:
                    if "urls.py" in subfiles:
                        settings_dir = os.path.join(manage_dir, f)
                        break

    return settings_dir


__all__ = ("django_dottedstring_imports", "django_find_root_dir")
